﻿using HCDV.HCSP;
using NPinyin;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace hcrainMvc.Controllers
{
    public class TestController : Controller
    {
        #region C#汉字转拼音(npinyin)将中文转换成拼音全文或首字母
        /// <summary>
        /// 拼音全文
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        [ActionName("Test1")]
        [HttpGet]
        public string Test1(string s)
        {
            return Pinyin.GetPinyin(s);
        }
        /// <summary>
        /// 首字母
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        [ActionName("Test2")]
        [HttpGet]
        public string Test2(string s)
        {
            Encoding gb2312 = Encoding.GetEncoding("GB2312");
            string v = Pinyin.ConvertEncoding(s, Encoding.UTF8, gb2312);
            return Pinyin.GetInitials(v, gb2312);

        }
        [ActionName("Test3")]
        [HttpGet]
        public string Test3()
        {
            Encoding gb2312 = Encoding.GetEncoding("GB2312");

            DataTable dt = null;
            using (dbsimple db = new dbsimple())
            {
                dt = db.Query("select * from yw_baseinfo where type=1 and maintype=1");

                foreach (DataRow dr in dt.Rows)
                {
                    string v = Pinyin.ConvertEncoding(dr["fv1"].ToString().Replace("有机",""), Encoding.UTF8, gb2312);
                    var fv7 = Pinyin.GetInitials(v, gb2312);
                    db.exe(string.Format("update yw_baseinfo set fv7='{0}' where id={1}",fv7,dr["id"].ToString()));
                }
            }
            return "OK";

        }

        public void test1()
        {
            string[] maxims = new string[]{
        "事常与人违，事总在人为",
        "骏马是跑出来的，强兵是打出来的",
        "驾驭命运的舵是奋斗。不抱有一丝幻想，不放弃一点机会，不停止一日努力。 ",
        "如果惧怕前面跌宕的山岩，生命就永远只能是死水一潭", 
        "懦弱的人只会裹足不前，莽撞的人只能引为烧身，只有真正勇敢的人才能所向披靡"
      };

            string[] medicines = new string[] {
        "聚维酮碘溶液",
        "开塞露",
        "炉甘石洗剂",
        "苯扎氯铵贴",
        "鱼石脂软膏",
        "莫匹罗星软膏",
        "红霉素软膏",
        "氢化可的松软膏",
        "曲安奈德软膏",
        "丁苯羟酸乳膏",
        "双氯芬酸二乙胺乳膏",
        "冻疮膏",
        "克霉唑软膏",
        "特比奈芬软膏",
        "酞丁安软膏",
        "咪康唑软膏、栓剂",
        "甲硝唑栓",
        "复方莪术油栓"
      };

            Console.WriteLine("UTF8句子拼音：");
            foreach (string s in maxims)
            {
                Console.WriteLine("汉字：{0}\n拼音：{1}\n", s, Pinyin.GetPinyin(s));
            }

            Encoding gb2312 = Encoding.GetEncoding("GB2312");
            Console.WriteLine("GB2312拼音简码：");
            foreach (string m in medicines)
            {
                string s = Pinyin.ConvertEncoding(m, Encoding.UTF8, gb2312);
                Console.WriteLine("药品：{0}\n简码：{1}\n", s, Pinyin.GetInitials(s, gb2312));
            }

            Console.ReadKey();
        }


        #endregion
        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View();
        }

    }
}
